var rootPath = getRootPath();

$(function() {
	$.ajax({
		url: rootPath + '/role/selectPageRoles',
        type: 'POST',
        contentType : 'application/json',
        data: JSON.stringify({
        	"dr" : 1
        }),
        success: function (data) {
        	fillRole(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
        	layer.alert("error:(");
        }
    });
});

function fillRole(data) {
	if(data.returnCode == "0000") {
		var length = data.returnObject.list.length;
		$("#dataNum").html(data.returnObject.total);
		var tr = "";
		for(var i = 0;i < length;i++) {
			tr += "<tr class='text-c' id='role_" + data.returnObject.list[i].rleId +"' name='rleTr'>";
			tr += "<td>" + data.returnObject.list[i].rleName + "</td>";
			tr += "<td>" + ((data.returnObject.list[i].verify == 1) ? "是" : "否") + "</td>";
		    tr += "<td>" + formatUndefined(data.returnObject.list[i].rleDesc) + "</td><td class='f-14'>";
//			tr += "<a title='编辑' href='javascript:;' onclick='role_add_edit(\"编辑角色\"," + "\"roleEditPage?rleId=" + data.returnObject.list[i].rleId + "\",\"\",\"\")' style='text-decoration:none'><i class='Hui-iconfont'>&#xe6df;</i></a>";
//			tr += "<a title='删除' href='javascript:;' onclick='role_del(this," + "\"" + data.returnObject.list[i].rleId + "\")' class='ml-5' style='text-decoration:none'><i class='Hui-iconfont'>&#xe6e2;</i></a></td></tr>";
			tr += "<a title='编辑' href='javascript:;' onclick='role_add_edit(\"编辑角色\"," + "\"roleEditPage?rleId=" + data.returnObject.list[i].rleId + "\",\"\",\"\")' style='text-decoration:none'><i class='iconfont marginLeft12'>&#xe60a;</i></a></td></tr>";
		}
		$("#roles").empty();
		$("#roles").append(tr);
		
		var page = "";
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ 1 + ")'>首页</span>";
		if (data.returnObject.hasPreviousPage) {
			page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.prePage + ")'>上一页</span>";
		}
		var num = data.returnObject.navigatepageNums.length;
		for (var i = 0; i < num; i++) {
			if (data.returnObject.navigatepageNums[i] == data.returnObject.pageNum) {
				page += "<span style='font-weight:900;padding:7px 10px;'>"
						+ data.returnObject.pageNum + "</span>";
			} else {
				page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
						+ data.returnObject.navigatepageNums[i]
						+ ")'>"
						+ data.returnObject.navigatepageNums[i] + "</span>";
			}
		}
		if (data.returnObject.hasNextPage) {
			page += "<span style='padding:7px 10px;' class='pageNavigate' onclick='switchPage("
					+ data.returnObject.nextPage + ")'>下一页</span>";
		}
		page += "<span style='padding:7px 10px' class='pageNavigate' onclick='switchPage("
				+ data.returnObject.pages + ")'>末页</span>";

		$("#page").empty();
		$("#page").append(page);
		
		$(".pageNavigate").mouseover(function() {
			$(this).css("cursor", "pointer");
		});
	} else {
		layer.alert(data.returnMessage);
	}
}

function switchPage(pageNo) {
	$(this).css("cursor", "pointer");
	var dr = 1;
	$.ajax({
		url : rootPath + '/role/selectPageRoles',
		type : 'POST',
		contentType : 'application/json',
		data : JSON.stringify({
			"dr" : dr,
			"pageNo" : pageNo
		}),
		success : function(data) {
			fillRole(data);
			$(".pageNavigate").hover(function() {
				$(this).css("cursor", "pointer");
			});
		},
		error : function(jqXHR, textStatus, errorThrown) {
			layer.alert("error:(");
		}
	});
}

function role_del(obj,id){
	var dataNum = parseInt($("#dataNum").html());
	layer.confirm('角色删除须谨慎，确认要删除吗？',function(index){
		if(id != "") {
			$.ajax({
				url: rootPath + '/role/deleteRole?rleId=' + id,
				type: 'GET',
				dataType: 'json',
				success: function(data){
					if(data.returnCode == "0000") {						
						$(obj).parents("tr").remove();
						layer.msg('已删除!',{icon:1,time:1000});
						
						$("#dataNum").html(dataNum - 1);
					} else {
						layer.alert(data.returnMessage);
					}
				},
				error:function(data) {
					layer.aler("error:(");
				},
			});		
		}
	});
}

/*管理员-角色-添加-编辑*/
function role_add_edit(title,url,w,h){
	layer_show(title,url,w,h);
}